package com.sumbo.config.pipeline;

import com.sumbo.config.processor.Processor;
import com.sumbo.config.writer.OutputContext;
import com.sumbo.config.writer.WriterFactory;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

/**
 * \* User: MeiZhongHao
 * \* Date: 2019-06-10
 * \* Time: 15:47
 * \* Description: 管道分流
 * \
 */
public class PipelineDispatch {

    public static boolean dispatch(String reader, String msg) {
        Collection<Pipeline> pipelines = PipelineConfig.getPipelineSearch(reader);

        if (pipelines == null || pipelines.isEmpty()) {
            return true;
        }
        for (Pipeline pipeline : pipelines) {
            try {
                // do processor
                Processor processor = pipeline.getProcessorBean();
                if (processor == null) {
                    break;
                }
                String outMsg = processor.handle(msg);
                // send msg
                if (CollectionUtils.isEmpty(pipeline.getOutput())) {
                    break;
                }
                // output
                for (OutputContext out : pipeline.getOutput()) {
                    WriterFactory.send(out, outMsg);
                }
            } catch (Exception e) {
            }
        }
        return true;
    }
}